Android tarafı için yeni bir class oluştur DataSender adında alttaki kodu yaz
import android.os.AsyncTask;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class DataSender extends AsyncTask<String,Void,Void> {
DataOutputStream dos;
PrintWriter pw;
Socket s;
@Override
protected Void doInBackground(String... voids) {
String Str=voids[0];
try {
pw=new PrintWriter(s.getOutputStream());
pw.write(Str);
pw.flush();
pw.close();
s.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
return null;
}
}
Kodu çağırmak için MainActivity kısmında buton click yöntemine bu kodu yaz
public void Gonder(View v)
{
DataSender ds=new DataSender();
ds.execute("gönderilecek bilgi");
}
C# kısmında
class ServerTCP
{
static TcpListener sevrevSoket = new TcpListener(IPAddress.Any, 6060);
public static void InitalizeNetWork()
{
sevrevSoket.Start();
sevrevSoket.BeginAcceptSocket(new AsyncCallback(OnClientConnect), null);
}
private static void OnClientConnect(IAsyncResult result)
{
TcpClient client = sevrevSoket.EndAcceptTcpClient(result);
sevrevSoket.BeginAcceptSocket(new AsyncCallback(OnClientConnect), null);
ClientManeger.CreateConnection(client);
}
}
static class ClientManeger
{
public static void CreateConnection(TcpClient tempClient)
{
Client newClient = new Client();
newClient.soket = tempClient;
newClient.ConnectionID = ((IPEndPoint)tempClient.Client.RemoteEndPoint).Port;
newClient.Start();
}
}
class Client
{
public int ConnectionID;
public TcpClient soket;
public NetworkStream stream;
private byte[] recBuffer;
public ByteBuffer buffer;
public void Start()
{
soket.SendBufferSize = 4096;
soket.ReceiveBufferSize = 4096;
stream = soket.GetStream();
recBuffer = new byte[4096];
stream.BeginRead(recBuffer, 0, soket.ReceiveBufferSize, Onrecivedata, null);
}
private void Onrecivedata(IAsyncResult result)
{
try
{
int lengt = stream.EndRead(result);
if (lengt <= 0)
{
CloseConnection();
return;
}
byte[] newbytes = new byte[lengt];
Array.Copy(recBuffer, newbytes, lengt);
string str = Encoding.UTF8.GetString(recBuffer);
MessageBox.Show(str);
stream.BeginRead(recBuffer, 0, soket.ReceiveBufferSize, Onrecivedata, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
CloseConnection();
}
}
private void CloseConnection()
{
Sabitler.CikisEkle(ConnectionID);
SqlManagement sql = new SqlManagement();
sql.DeleteOnline(ConnectionID);
soket.Close();
}
}
bende çalışmıştı bu kodlar eksik bişey yazmadıysam bu şekilde çalışıyor